/*
** Mortem's HGE Addon 1.3
** Copyright (C) 2006-2007, Osokin 'elmortem' Makar
** http://elmortem.mirgames.ru
**
** MHA GUI loader.
*/

#include <mhaguiloader.h>

#include <mhautils.h>
#include <stdio.h>

HGE *mhaGuiLoader::hge = 0;

mhaGuiLoader::mhaGuiLoader(const HGE_CHAR *filename, hgeGUI *gui, hgeResourceManager *resource, hgeStringTable *strings) {
	hge = hgeCreate(HGE_VERSION);
	Gui = gui;
	Res = resource;
	Str = strings;
	Ini = new mhaIni(hge->Resource_MakePath(filename));
}

mhaGuiLoader::~mhaGuiLoader(void) {
	delete Ini;

	hge->Release();
}

void mhaGuiLoader::Add(int id, int type, const HGE_CHAR *section, int param) {
	//HGE_CHAR sVal[MAX_PATH];
	switch(type) {
		case MHAGUI_WINDOW:
			Gui->AddCtrl(new mhaGuiWindow(id, Ini->GetInt(section, TXT("shadow"), 0), Ini->GetFloat(section, TXT("x"), hge->System_GetState(HGE_SCREENWIDTH)/2), Ini->GetFloat(section, TXT("y"), hge->System_GetState(HGE_SCREENHEIGHT)/2), Ini->GetFloat(section, TXT("tex.x"), 0), Ini->GetFloat(section, TXT("tex.y"), 0), Ini->GetFloat(section, TXT("tex.width"), hge->System_GetState(HGE_SCREENWIDTH)), Ini->GetFloat(section, TXT("tex.height"), hge->System_GetState(HGE_SCREENHEIGHT)), (Res) ? (Res->GetTexture(Ini->GetString(section, TXT("tex"), TXT("default_tex")))) : (0), (bool)Ini->GetInt(section, TXT("center"), (int)true)));
		break;
		case MHAGUI_CHECK:
			Gui->AddCtrl(new mhaGuiCheck(id, (Res) ? (Res->GetFont(Ini->GetString(section, TXT("font"), TXT("default_fnt")))) : (0), Ini->GetFloat(section, TXT("font.size"), 1.f), s2color(Ini->GetString(section, TXT("font.color.normal"), TXT("FFFFFFFF"))), Ini->GetFloat(section, TXT("x"), 400), Ini->GetFloat(section, TXT("y"), 200), Ini->GetFloat(section, TXT("tex.x"), 0), Ini->GetFloat(section, TXT("tex.y"), 0), Ini->GetFloat(section, TXT("tex.width"), 32), Ini->GetFloat(section, TXT("tex.height"), 48), (Res) ? (Res->GetTexture(Ini->GetString(section, TXT("tex"), TXT("default_tex")))) : (0), (Str) ? (Str->GetString(Ini->GetString(section, TXT("string"), TXT("default")))) : (Ini->GetString(section, TXT("string"), TXT("default"))), (mhaCallback*)param, (bool)Ini->GetInt(section, TXT("checked"), (int)false)));
		break;
		case MHAGUI_TEXTBUTTON:
			Gui->AddCtrl(new mhaGuiTextButton(id, (Res) ? (Res->GetFont(Ini->GetString(section, TXT("font"), TXT("default_fnt")))) : (0), Ini->GetFloat(section, TXT("shadow"), 2.f), Ini->GetFloat(section, TXT("font.size"), 1.f), s2color(Ini->GetString(section, TXT("font.color.normal"), TXT("FFFFFFFF"))), s2color(Ini->GetString(section, TXT("font.color.over"), TXT("FFFFFFFF"))), Ini->GetFloat(section, TXT("x"), 395), Ini->GetFloat(section, TXT("y"), 340), Ini->GetFloat(section, TXT("tex.x"), 0), Ini->GetFloat(section, TXT("tex.y"), 0), Ini->GetFloat(section, TXT("tex.width"), 32), Ini->GetFloat(section, TXT("tex.height"), 48), Ini->GetFloat(section, TXT("width"), 100), Ini->GetInt(section, TXT("align"), 1), (Res) ? (Res->GetTexture(Ini->GetString(section, TXT("tex"), TXT("default_tex")))) : (0), (Str) ? (Str->GetString(Ini->GetString(section, TXT("string"), TXT("default")))) : (Ini->GetString(section, TXT("string"), TXT("default"))), (mhaCallback*)param));
		break;
		case MHAGUI_INPUT:
			//HGE_SNCPY(sVal, , MAX_PATH);
			Gui->AddCtrl(new mhaGuiInput(id, (Res) ? (Res->GetFont(Ini->GetString(section, TXT("font"), TXT("default_fnt")))) : (0), Ini->GetFloat(section, TXT("font.size"), 1.f), Ini->GetFloat(section, TXT("shadow"), 1.f), Ini->GetFloat(section, TXT("x"), 400), Ini->GetFloat(section, TXT("y"), 200), Ini->GetFloat(section, TXT("width"), 200), s2color(Ini->GetString(section, TXT("font.color"), TXT("FFFFFFFF"))), Ini->GetInt(section, TXT("max"), 10), 0));
			mhaGetInputCtrl(Gui, id)->SetText(Ini->GetString(section, TXT("string"), TXT("default")));
		break;
		case MHAGUI_SCROLL:
			Gui->AddCtrl(new mhaGuiScroll(id, Ini->GetFloat(section, TXT("x"), 395), Ini->GetFloat(section, TXT("y"), 340), Ini->GetFloat(section, TXT("width"), 340), Ini->GetFloat(section, TXT("tex.x"), 0), Ini->GetFloat(section, TXT("tex.y"), 0), Ini->GetFloat(section, TXT("tex.width"), 32), Ini->GetFloat(section, TXT("tex.height"), 32), (Res) ? (Res->GetTexture(Ini->GetString(section, TXT("tex"), TXT("default_tex")))) : (0), (bool)Ini->GetInt(section, TXT("vertical"), (int)false)));
			mhaGetScrollCtrl(Gui, id)->SetMode(Ini->GetFloat(section, TXT("mode.min"), 0), Ini->GetFloat(section, TXT("mode.max"), 100));
			mhaGetScrollCtrl(Gui, id)->SetValue(Ini->GetFloat(section, TXT("value"), 0));
		break;
		case MHAGUI_TEXT:
			Gui->AddCtrl(new mhaGuiText(id, (Res) ? (Res->GetFont(Ini->GetString(section, TXT("font"), TXT("default_fnt")))) : (0), Ini->GetFloat(section, TXT("font.size"), 1.f), s2color(Ini->GetString(section, TXT("font.color"), TXT("FFFFFFFF"))), Ini->GetInt(section, TXT("shadow"), 2), Ini->GetFloat(section, TXT("x"), 400), Ini->GetFloat(section, TXT("y"), 200), Ini->GetFloat(section, TXT("width"), 300), Ini->GetFloat(section, TXT("height"), 200),  Ini->GetInt(section, TXT("align"), 2), (Str) ? (Str->GetString(Ini->GetString(section, TXT("string"), TXT("default")))) : (Ini->GetString(section, TXT("string"), TXT("default")))));
		break;
		case MHAGUI_LISTBOX:
			Gui->AddCtrl(new mhaGuiListbox(id, Ini->GetFloat(section, TXT("x"), 395), Ini->GetFloat(section, TXT("y"), 340), Ini->GetFloat(section, TXT("width"), 340), Ini->GetFloat(section, TXT("height"), 340), (Res) ? (Res->GetFont(Ini->GetString(section, TXT("font"), TXT("default_fnt")))) : (0), Ini->GetFloat(section, TXT("font.size"), 1.f), s2color(Ini->GetString(section, TXT("font.color.normal"), TXT("FFFFFFFF"))), s2color(Ini->GetString(section, TXT("font.color.select"), TXT("FFFF0000"))), s2color(Ini->GetString(section, TXT("font.color.bg"), TXT("99FFFFFF"))), true));
		break;
	}
}
